package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVO;
import com.atguigu.eduservice.entity.vo.CourseInfoVO;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-05-15
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    //课程大纲列表.根据课程id进行查询所有的章节
    @Override
    public List<EduChapter> getChapterVideoByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper();
        wrapperChapter.eq("course_id",courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(wrapperVideo);

        //封装树形结构
        List<EduChapter> root = new ArrayList<>();
        // 创建一个map对象用来存储course_id和EduVideo对象的关系
        Map<String, EduChapter> eduChapterMap = new HashMap<>();
        for (EduChapter eduChapter:
                eduChapterList) {
            String id = eduChapter.getId();
            eduChapterMap.put(id,eduChapter);
            root.add(eduChapter);
        }
        for (EduVideo eduVideo:
                eduVideoList) {
            // 获取当前menu对象的pid
            String pid = eduVideo.getChapterId();

            // 如果pid不为null，说明当前节点有父节点，menuMap找到父节点
            EduChapter chapterFather = eduChapterMap.get(pid);
//            System.out.println(chapterFather);
            // 将当前节点存入父节点
            if (chapterFather != null) {
                chapterFather.getChildren().add(eduVideo);
            }


        }
        return root;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        //根据id查询小节，如果查询出就不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        if (count > 0){
            throw new GuliException(20001,"请先删除完小节");
        }else{
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }
    }

    @Override
    public void removeChapterCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);

        baseMapper.delete(wrapper);
    }

}
